#!/usr/bin/env python3

# this is a dbus client.

import sys
from threading import Timer
import logging

logging.basicConfig(level=logging.DEBUG)

from PyQt5.QtCore import QCoreApplication, pyqtSlot, QObject
from PyQt5.QtDBus import QDBusAbstractInterface, QDBusConnection





class CarProxy(QDBusAbstractInterface):
    def __init__(self, service, path, connection, parent=None):
        super(CarProxy, self).__init__(service, path,
                'org.HallGarden.Examples.Interface', connection, parent)

    def turnLeft(self, degree):
        logging.debug("Lefting...")
        self.asyncCall('turn_left', degree)

    def turnRight(self):
        logging.debug("Righting...")
        self.asyncCall('turn_right')

    def turnBack(self):
        logging.debug("Backing...")
        self.asyncCall('turn_back')


class Controller(QObject):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.car = CarProxy('org.example.CarExample', '/Car/test',
                                QDBusConnection.sessionBus(), self)

    def run(self):
        if input("Choose the way to drive[left/right]: ") == "left":
            self.car.turnLeft(23)
        else:
            # self.car.car.turnRight()
            self.car.turnBack()


if __name__ == '__main__':
    # app = QCoreApplication(sys.argv)
    controller = Controller()
    controller.run()

    # Timer(5, timer_ask, [controller])
    # import time
    # time.sleep(30)

    # sys.exit(app.exec_())
